//
//  MineViewController.m
//  MiaoTu
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MTMacro.h"
#import "Masonry.h"
#import "MineTableViewCell.h"
#import "MineModel.h"
#import "Items.h"
#import "EditDataViewController.h"
#import "ConcernAndFanViewController.h"
#import "Login.h"
#import "FindFriendViewController.h"
#import "PersonalHomeViewController.h"
#import "ZWYNetTool.h"
#import "SettingViewController.h"

#define kMineTableViewCell @"MineTableViewCellIdentifier"

@interface MineViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSArray *> *mineArray;
@property (nonatomic, strong) MineHeaderView *headerView;

@end

@implementation MineViewController

- (void)setLogin:(Login *)login {
    _login = login;
    _headerView.login = _login;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"我的";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNotification:) name:@"LoginNotification" object:nil];
    
    [self createTableView];
    [self createData];
    [self headerClick];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:SCREEN_RECT style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    self.headerView = [MineHeaderView headerView];
    _headerView.login = _login;
    _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 156 / 602.f * SCREEN_HEIGHT);
    _tableView.tableHeaderView = _headerView;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kMineTableViewCell];
}

- (void)createData {
    self.mineArray = [NSMutableArray array];
    MineModel *personHome = [MineModel mineWithTitle:@"个人主页" picName:@"personalHomepage"];
    MineModel *myYueyou = [MineModel mineWithTitle:@"我发起的约游" picName:@"myYueyou"];
    [_mineArray addObject:@[personHome, myYueyou]];
    MineModel *myOrder = [MineModel mineWithTitle:@"我的订单" picName:@"myOrder"];
    MineModel *coupon = [MineModel mineWithTitle:@"优惠券" picName:@"coupon"];
    [_mineArray addObject:@[myOrder, coupon]];
    MineModel *setting = [MineModel mineWithTitle:@"设置" picName:@"setting"];
    [_mineArray addObject:@[setting]];
    
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    NSString *phoneStr = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhone"];
    NSString *passwordStr = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPassword"];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.miaotu.net/v2/base/user?phone=%@&password=%@", phoneStr, passwordStr];
    
    [ZWYNetTool GET:urlStr andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.login = [Login modelWithDic:result];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)headerClick {
    void (^sendButtonAction)(MineHeaderClickType clickType) = ^(MineHeaderClickType clickType) {
        NSLog(@"%lu", clickType);
        if (clickType == MineHeaderClickEditData) {
            EditDataViewController *editDataVC = [[EditDataViewController alloc] init];
            [self.navigationController pushViewController:editDataVC animated:YES];
        } else if (clickType == MineHeaderClickFindFriend) {
            FindFriendViewController *findFriendVC = [[FindFriendViewController alloc] init];
            [self.navigationController pushViewController:findFriendVC animated:YES];
        } else {
            ConcernAndFanViewController *conAndFanVC = [[ConcernAndFanViewController alloc] initWithNibName:@"ConcernAndFanViewController" bundle:[NSBundle mainBundle]];
            conAndFanVC.clickType = clickType;
            [self.navigationController pushViewController:conAndFanVC animated:YES];
        }
    };
    _headerView.sendButtonAction = sendButtonAction;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _mineArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mineArray[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineTableViewCell forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.mineModel = _mineArray[indexPath.section][indexPath.row];
    if (indexPath.section == 1 && indexPath.row == 1) {
        cell.couponLabel.hidden = NO;
        // 设置富文本        
        cell.couponLabel.attributedText = [self setRichText];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (NSMutableAttributedString *)setRichText {
    NSString *numberStr = @(_login.Items.CouponCount).stringValue;
    NSMutableAttributedString *attrubutedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@张", numberStr]];
    NSDictionary *numberAttriDic = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
                                     NSForegroundColorAttributeName: [UIColor colorWithRed:0.918 green:0.435 blue:0.000 alpha:1.000]};
    NSDictionary *otherAttriDic = @{NSFontAttributeName: [UIFont systemFontOfSize:14],
                                    NSForegroundColorAttributeName: [UIColor colorWithWhite:0.518 alpha:1.000]};
    [attrubutedStr setAttributes:numberAttriDic range:NSMakeRange(0, attrubutedStr.length - @"张".length)];
    [attrubutedStr setAttributes:otherAttriDic range:NSMakeRange(attrubutedStr.length - @"张".length, @"张".length)];
    return attrubutedStr;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            NSLog(@"个人主页");
            PersonalHomeViewController *personHomeVC = [[PersonalHomeViewController alloc] init];
            personHomeVC.Uid = _login.Items.Uid;
            personHomeVC.isOtherInfo = NO;
            [self.navigationController pushViewController:personHomeVC animated:YES];
        } else if (indexPath.row == 1) {
            NSLog(@"我发起的约游");
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NSLog(@"我的订单");
        } else if (indexPath.row == 1) {
            NSLog(@"优惠券");
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            SettingViewController *settingVC = [[SettingViewController alloc] init];
            [self.navigationController pushViewController:settingVC animated:YES];
            NSLog(@"设置");
        }
    }
}

- (void)loginNotification:(NSNotification *)notice {
    _login = notice.userInfo[@"loginModel"];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LoginNotification" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
